package com.fast.framework.web.plugin.redis;


import com.fast.framework.web.plugin.redis.serializer.ISerializer;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JfastRedis {

	private JedisPool jedisPool;
	private static final ThreadLocal<Jedis> threadLocal = new ThreadLocal<Jedis>();
	private ISerializer iSerializer;
	
	public JfastRedis(ISerializer iSerializer,JedisPool jedisPool){
		this.iSerializer = iSerializer;
		this.jedisPool = jedisPool;
	}
	
	public JfastRedis(JedisPool jedisPool){
		this.jedisPool = jedisPool;
	}
	
	public String set(Object key,Object value){
		Jedis jedis = getJedis();
		return jedis.set(keyToBytes(key), valueToBytes(key));
	}
	
	protected byte[] keyToBytes(Object key){
		return null;
	}
	
    protected byte[] valueToBytes(Object value){
		return null;
	}

    public void setLocalRedis(Jedis jedis){
    	threadLocal.set(jedis);
    }
    
	public ISerializer getiSerializer() {
		return iSerializer;
	}

	public void setiSerializer(ISerializer iSerializer) {
		this.iSerializer = iSerializer;
	}

	public Jedis getThreadLocal() {
		return threadLocal.get();
	}

	public Jedis getJedis() {
		Jedis jedis = threadLocal.get();
		return jedis == null ? jedisPool.getResource() : jedis;
	}
}
